<?php

class DescuentoController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete',
			);
	}



	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			/*array('allow',  
				'actions'=>array('index','view','assign'),
				'users'=>array('*'),
				),*/
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('index','ListarEstados','dele','admin'),
			    //'users'=>array('@'),
				'roles'=>array('Adm_Bodega'),
				),
			/*array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('@'),
				),*/
			array('deny',  // deny all users
				'users'=>array('*'),
				),
			);
	}

	
	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model=new Descuento;
		$model->generardb();//crea tabla temporal con funcion creada en el modelo.

		//aplicar descuento
		if(isset($_POST['Aplicar'])){//Aplicar cambios y ajustes.
			$model->ajustar();
			$this->redirect(array('index'));
		}

		if (isset($_POST['Descuento'])) 
		{
			$model->attributes = $_POST['Descuento'];
			if($model->validate())
			{
				$model->insertartemp();
					$model = new Descuento;
				
				
			}
		}
		$this->render('index', array('model' => $model, ));
	}
// accion eliminar
	public function actionDele($id)
	{
	 
	   $model=new Descuento;
	   $model->eliminar($id);

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));

	  	
	}
	public function loadModel($id)
	{
		$model=Descuento::model()->findAll($id);
		if($model===null)
			throw new CHttpException(404,'La página solicitada no existe.');
		return $model;
	}

	public function actionListarEstados($term) {
		$criteria = new CDbCriteria;
		$criteria->condition = "LOWER(Pr_idproducto_PK) like LOWER(:term)";
		$criteria->params = array(':term'=> '%'.$_GET['term'].'%');
		$criteria->limit = 30;
		$model = catalogo::model()->findAll($criteria);
		$arr = array();
		foreach ($model as $item) {
			$arr[] = array(
				'label' => $item->Pr_idproducto_PK,
				'value' => $item->Pr_idproducto_PK,
				'id' => $item->Pr_idproducto_PK,


				);
		}
		echo CJSON::encode($arr);
	}
}
